<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../styles/booksList.css" />
<title>图书列表</title>
</head>
<body>
<?php
include '../includes/dbhelper.php';
//$conn = mysql_connect ( "localhost", "root", "111111" ) or die ( "数据库连接错误" );
//mysql_select_db ( "bookmgr", $conn ) or die ( "数据库访问错误" );
//mysql_query ( "set names utf8" );
if (is_numeric ( $_GET ['bookid'] )) {
	$bookid = ( int ) $_GET ['bookid'];
	$sql = "DELETE FROM `bookmgr_book_info` WHERE `bookid`='$bookid'";
	$result2 = mysql_query ( $sql );
	if (! ! $result2) {
		echo "<script>alert('删除成功')</script>";
	} else {
		echo "<script>alert('删除失败')</script>";
	}
}
// 每页的显示条数
$page_size = 6;
// 当前页码
$page_num = 1;
if (is_numeric ( $_GET ['page'] )) {
	$page_num = ( int ) $_GET ['page'];
}
// 总条数
$resultA = mysql_query ( "select count(bookid) as total from bookmgr_book_info" );
$data_count = mysql_result ( $resultA, 0, "total" );
// 总页数
$page_count = ceil ( $data_count / $page_size );

// 当前页码中的数据
$offset = ($page_num - 1) * $page_size;
$resultB = mysql_query ( "select * from bookmgr_book_info where 1=1 order by _id asc limit $offset , $page_size" );
$row = mysql_fetch_array ( $resultB );
?>
	<form action="./booksList.php" target="frm" method="get">
		<div>
			<h1>图书列表</h1>
			<table class="tb_list">
				<tr>
					<th>图书ID</th>
					<th>书&nbsp;&nbsp;名</th>
					<th>价&nbsp;&nbsp;格</th>
					<th>删除</th>
				</tr>
				<?php
				// 创建页面元素数据
				if (! $row) {
					echo "<tr><td colspan='3'>暂无数据</td></tr>";
				} else {
					do {
						echo "<tr>";
						echo "<td><a href='addBooks.php?_id=" . $row ['_id'] . "'>" . $row ['bookid'] . "</a></td>";
						echo "<td>" . $row ['bookname'] . "</td>";
						echo "<td>" . $row ['bookprice'] . "</td>";
						echo "<td><a href='booksList.php?bookid=" . $row ['bookid'] . "'>删除</a></td>";
						echo "</tr>";
					} while ( ! ! ($row = mysql_fetch_array ( $resultB )) );
				}
				?>
			</table>
		</div>
		<div class="table_style">
			<table class="tb_pager">
				<tr>
					<td class="td1"><?php
					if ($page_num > 1) {
						echo "<a href='booksList.php?page=1' target='_self'>首页</a>&nbsp;";
						echo "<a href='booksList.php?page=" . ($page_num - 1) . "' target='_self'>上一页</a>&nbsp;";
					}
					if ($page_num < $page_count) {
						echo "<a href='booksList.php?page=" . ($page_num + 1) . "' target='_self'>下一页</a>&nbsp;";
						echo "<a href='booksList.php?page=" . $page_count . "' target='_self'>尾页</a>&nbsp;";
					}
					?></td>
					<td class="td2">当前页<?php echo $page_num."/".$page_count; ?></td>
					<td class="td2">共<?php echo $data_count; ?>条记录</td>
				</tr>
			</table>
			<h2></h2>
			<div class="table_style">
				<label> 跳转到： </label> <input type="text" name="page">页</input> <input
					class="button" name="user_submit" type="submit" value="跳转" />
			</div>
		</div>
	</form>
</body>
</html>
